package com.xgst.land.viewmodel

import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.hjq.http.EasyHttp
import com.hjq.http.listener.OnHttpListener
import com.xgst.land.base.BaseViewModel
import com.xgst.land.bean.PDOrderDetailBean
import com.xgst.land.bean.PDQueryBoxBean
import com.xgst.land.bean.QuerySampleBean
import com.xgst.land.bean.SaveSampleScanBean
import com.xgst.land.bean.SuccessBean
import com.xgst.land.http.api.FindSampleAPI
import com.xgst.land.http.api.RequestSampleBoxAPI
import com.xgst.land.http.api.SampleDeleteAPI
import com.xgst.land.http.api.SampleRevise
import com.xgst.land.http.api.SampleSave
import java.lang.Exception

class BuildOrderViewModel: BaseViewModel() {
    //查询样本盒
    fun findSample(projectId:String,sampleName:String?,sampleCategoryId:String?
                         ,onSuccess:(MutableList<QuerySampleBean.DataBean>)->Unit){
        EasyHttp.post(this)
            .api(FindSampleAPI(projectId,sampleName,sampleCategoryId))
            .request(object : OnHttpListener<QuerySampleBean>{
                override fun onSucceed(result: QuerySampleBean) {
                    LogUtils.i("集合  "+result.data.size)
                    onSuccess(result.data)
                }

                override fun onFail(e: Exception) {
                    dismissProgress(e.message?:"querySampleBoxListByCondition 错误")
                }
            })
    }

    fun reuqestSampleBox(id:String,onSuccess:(PDOrderDetailBean.DataBean)->Unit){
        showProgress()
        EasyHttp.post(this)
            .api(RequestSampleBoxAPI(id))
            .request(object : OnHttpListener<PDOrderDetailBean>{
                override fun onSucceed(result: PDOrderDetailBean) {
                    dismissProgress()
                    onSuccess(result.data)
                }

                override fun onFail(e: Exception) {
                    dismissProgress(e.message?:"")
                }
            })
    }

    //删除盘点计划中的样本盒
    fun delete(inventorySampleBoxIdList:String,onResult:()->Unit){
        showProgress()
        EasyHttp.post(this)
            .api(SampleDeleteAPI(inventorySampleBoxIdList))
            .request(object : OnHttpListener<SuccessBean>{
                override fun onSucceed(result: SuccessBean) {
                    onResult()
                }

                override fun onFail(e: Exception) {
                    dismissProgress(e.message?:"")
                }
            })
    }

    fun save(name:String,rfidSampleBoxIdList:String,onResult:()->Unit){
        EasyHttp.post(this)
            .api(SampleSave(TimeUtils.getNowString(),name,rfidSampleBoxIdList))
            .request(object : OnHttpListener<SaveSampleScanBean>{
                override fun onSucceed(result: SaveSampleScanBean) {
                    onResult()
                }

                override fun onFail(e: Exception) {
                    dismissProgress(e.message?:"")
                }
            })
    }

    //修改
    fun revise(id:String,name:String,rfidSampleBoxIdList:String,onResult:()->Unit){
        EasyHttp.post(this)
            .api(SampleRevise(id,name,rfidSampleBoxIdList))
            .request(object : OnHttpListener<SuccessBean>{
                override fun onSucceed(result: SuccessBean) {
                    onResult()
                }

                override fun onFail(e: Exception) {
                    dismissProgress(e.message?:"")
                }
            })
    }

}